<?php

namespace ServiceSdk\Payment\Payunk;

use ServiceSdk\Libs\Remote;

class Payment extends Config
{
    // 请求参数
    public static $getRequestParams = '';

    /**
     * 通道01扫码支付
     *
     * @param array $payInfo
     *
     * @return array
     * @throws \Exception
     */
    public static function payWithShengH5(array $payInfo): array
    {
        try {
            $payConfig = self::payunkConfig();
            $payPostParams = [
                'appid'        => $payConfig['appKey'],
                'pay_type'     => 'adapayH5',
                'amount'       => sprintf("%.2f", $payInfo['money']),
                'callback_url' => $payInfo['notify_url'],
                'success_url'  => $payInfo['response_url'],
                'error_url'    => $payInfo['response_error_url'],
                'extend'       => json_encode(['subject' => $payInfo['subject']], 320),
                //            'version'      => $payConfig['version'],
                'out_trade_no' => $payInfo['order_sn'],
            ];
            $payPostParams['sign'] = self::getSign($payPostParams);
            $paramsJson = json_encode($payPostParams);
            self::$getRequestParams = $paramsJson;
            $response = Remote::postWithUrl($payConfig['createPayAlipayOrderUrl'], $paramsJson, 30);
            $response = is_array($response) ? $response : [$response];

            return $response;
        } catch (\Exception $e) {
            return [
                'code'    => 500,
                'message' => '支付服务有异常，异常信息：' . $e->getMessage() . '，文件信息：' . $e->getFile() . ':' . $e->getLine()
            ];
        }
    }

    /**
     * 通道02支付
     *
     * @param array $payInfo
     *
     * @return array
     * @throws \Exception
     */
    public static function payWithWeChatJs(array $payInfo): array
    {
        try{
            $payConfig = self::payunkConfig();
            $payPostParams = [
                'appid'        => $payConfig['appKey'],
                'pay_type'     => 'weChatJs',
                'amount'       => sprintf("%.2f", $payInfo['money']),
                'callback_url' => $payInfo['notify_url'],
                'success_url'  => $payInfo['response_url'],
                'error_url'    => $payInfo['response_error_url'],
                'extend'       => json_encode(['body' => $payInfo['subject']], 320),
                //            'version'      => $payConfig['version'],
                'out_trade_no' => $payInfo['order_sn'],
            ];
            $payPostParams['sign'] = self::getSign($payPostParams);
            $paramsJson = json_encode($payPostParams, 320);
            self::$getRequestParams = $paramsJson;
            $response = Remote::postWithUrl($payConfig['createWechatOrderUrl'], $paramsJson, 30);
            $response = is_array($response) ? $response : [$response];

            return $response;
        } catch (\Exception $e) {
            return [
                'code'    => 500,
                'message' => '支付服务有异常，异常信息：' . $e->getMessage() . '，文件信息：' . $e->getFile() . ':' . $e->getLine()
            ];
        }
    }

    /**
     * 通道02支付
     *
     * @param array $payInfo
     *
     * @return array
     * @throws \Exception
     */
    public static function payWithWeChatJssh(array $payInfo): array
    {
        try{
            $payConfig = self::payunkConfig();
            $payPostParams = [
                'appid'        => $payConfig['appKey'],
                'pay_type'     => 'weChatJssh',
                'amount'       => sprintf("%.2f", $payInfo['money']),
                'callback_url' => $payInfo['notify_url'],
                'success_url'  => $payInfo['response_url'],
                'error_url'    => $payInfo['response_error_url'],
                'extend'       => json_encode(['body' => $payInfo['subject']], 320),
                //            'version'      => $payConfig['version'],
                'out_trade_no' => $payInfo['order_sn'],
            ];
            $payPostParams['sign'] = self::getSign($payPostParams);
            $paramsJson = json_encode($payPostParams, 320);
            self::$getRequestParams = $paramsJson;
            $response = Remote::postWithUrl($payConfig['createWechatJsshOrderUrl'], $paramsJson, 30);
            $response = is_array($response) ? $response : [$response];

            return $response;
        } catch (\Exception $e) {
            return [
                'code'    => 500,
                'message' => '支付服务有异常，异常信息：' . $e->getMessage() . '，文件信息：' . $e->getFile() . ':' . $e->getLine()
            ];
        }
    }

    /**
     * 获取订单支付信息
     *
     * @param string $orderSn
     *
     * @return mixed
     * @throws \Exception
     */
    public static function checkPayOrder(string $orderSn)
    {
        $payConfig = self::payunkConfig();
        $payPostParams = [
            'appid'        => $payConfig['appKey'],
            'out_trade_no' => $orderSn
        ];
        $payPostParams['sign'] = self::getSign($payPostParams);
        $paramsJson = json_encode($payPostParams, 320);
        $response = Remote::postWithUrl($payConfig['checkOrderUrl'], $paramsJson, 30);

        return $response;
    }

    /**
     * 取消订单
     *
     * @param string $orderSn
     *
     * @return mixed
     * @throws \Exception
     */
    public static function cancelOrder(string $orderSn)
    {
        $payConfig = self::payunkConfig();
        $payPostParams = [
            'appid'        => $payConfig['appKey'],
            'out_trade_no' => $orderSn
        ];
        $payPostParams['sign'] = self::getSign($payPostParams);
        $paramsJson = json_encode($payPostParams, 320);
        $response = Remote::postWithUrl($payConfig['cancelOrderUrl'], $paramsJson, 30);

        return $response;
    }
}
